package com.jsbs.iam.user.service.impl;

import com.jsbs.iam.user.entity.IamApplyMenu;
import com.jsbs.iam.user.entity.IamButton;
import com.jsbs.iam.user.mapper.IamApplyMenuMapper;
import com.jsbs.iam.user.mapper.IamButtonMapper;
import com.jsbs.iam.user.service.IamApplyMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zhouxiangyang
 * @className ApplyMenuServiceImpl
 * @date 2022/12/12 20:48
 * @description:
 */
@Service
public class ApplyMenuServiceImpl implements IamApplyMenuService {

    @Autowired
    private IamButtonMapper iamButtonMapper;

    @Autowired
    private IamApplyMenuMapper iamApplyMenuMapper;

    /**
     * 获取一个用户下所有的菜单列表
     * @param userCode
     * @return
     */
    @Override
    public List<IamApplyMenu> getIamApplyMenuList(String userCode) {
        //查询用户下有哪些菜单编码
        List<String> menuCodeList = iamButtonMapper.getMenuCode(userCode);
        //根据菜单编码查询菜单列表
        List<IamApplyMenu> applyMenu = iamApplyMenuMapper.getApplyMenu(menuCodeList);
        //根据菜单层级编码分类
//        Map<String,List<IamApplyMenu>> map = new HashedMap<>();TODO 按照菜单层级存redis
//        applyMenu.forEach(iamApplyMenu ->{
//            String menuSuperiorCode = iamApplyMenu.getMenuSuperiorCode();
//            if (MapUtils.isNotEmpty(map)) {
//                List<IamApplyMenu> iamApplyMenus = map.get(menuSuperiorCode);
//                iamApplyMenus.add(iamApplyMenu);
//                map.put(menuSuperiorCode,iamApplyMenus);
//            } else {
//                List<IamApplyMenu> iamApplyMenus = new ArrayList<>();
//                iamApplyMenus.add(iamApplyMenu);
//                map.put(menuSuperiorCode,iamApplyMenus);
//            }
//        });
        return applyMenu;
    }

    @Override
    public List<IamButton> getButtonList(String applyCode) {
        List<IamButton> buttonList = iamButtonMapper.getButtonList(applyCode);
        return buttonList;
    }

    @Override
    public List<IamApplyMenu> queryByMenuCode(List<String> menuCodeList) {
        return iamApplyMenuMapper.queryByMenuCode(menuCodeList);
    }
}
